package com.starhub.api.sys.user.controller;

import com.starhub.api.sys.user.dto.PasswordDto;
import com.starhub.api.sys.user.dto.SysUserDto;
import com.starhub.api.sys.user.result.UserInfoResult;
import com.starhub.api.sys.user.service.LoginService;
import com.starhub.common.bean.ResultResp;
import com.starhub.common.security.util.SecurityUtils;
import com.starhub.common.sys.user.LoginUser;
import com.starhub.engine.sys.user.service.SysUserService;
import com.starhub.utils.Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@Api(description = "登录接口")
@RestController
@RequestMapping("/api/starhub/user")
public class LoginController {

    private static final Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @Autowired
    private SysUserService sysUserService;


    @PostMapping(value = "/login")
    @ApiOperation(value = "登录接口", notes = "登录接口")
    public ResultResp<Map<String, Object>> login(@RequestParam("username") String username, @RequestParam("password") String password) throws Exception {
        return ResultResp.success(loginService.login(username,password));
    }


    /**
     * 用户信息获取
     */
    @ApiOperation(value = "向量库设置信息获取", notes = "向量库设置信息获取")
    @GetMapping("/getInfo/{id}")
    public ResultResp getInfo(@PathVariable Long id){
        ResultResp resultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            resultResp = ResultResp.success(sysUserService.getInfo(user, id));
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
            resultResp = ResultResp.fail(e.getMessage(),e);
        }
        return resultResp;
    }

    /**
     * 获取当前登录用户信息
     */
    @ApiOperation(value = "获取当前登录用户信息", notes = "获取当前登录用户信息")
    @GetMapping("/current")
    public ResultResp<UserInfoResult> getCurrentUser() {
        try {
            LoginUser loginUser = SecurityUtils.getCurrentUser();
            SysUserDto user = sysUserService.getInfo(loginUser, loginUser.getUser().getId());//loginUser.getUser();
            SysUserDto userInfo = SysUserDto.builder()
                .id(user.getId())
                .username(user.getUsername())
                .name(user.getName())
                .email(user.getEmail())
                .phone(user.getPhone())
                .avatar(user.getAvatar())
                .roleId(user.getRoleId())
                .gender(user.getGender())
                .build();
            return ResultResp.success(UserInfoResult.success(userInfo));
        } catch (Exception e) {
            log.error("获取当前用户信息失败", e);
            return ResultResp.fail("获取用户信息失败：" + e.getMessage(),e);
        }
    }

    /**
     * 用户设置修改
     */
    @ApiOperation(value = "用户设置修改", notes = "用户设置修改")
    @PostMapping("/updatePassword")
    public ResultResp updatePassword(@RequestBody PasswordDto passwordDto){
        ResultResp resultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            resultResp = ResultResp.success(sysUserService.updatePassword(user, passwordDto));
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
            resultResp = ResultResp.fail(e.getMessage(),e);
        }
        return resultResp;
    }

    /**
     * 用户设置修改
     */
    @ApiOperation(value = "用户设置修改", notes = "用户设置修改")
    @PostMapping("/updateUser")
    public ResultResp updateUser(@RequestBody SysUserDto sysUserDto){
        ResultResp resultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            resultResp = ResultResp.success(sysUserService.updateUser(user, sysUserDto));
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
            resultResp = ResultResp.fail(e.getMessage(),e);
        }
        return resultResp;
    }


    /**
     * 用户设置列表获取
     */
    @ApiOperation(value = "用户设置列表获取", notes = "用户设置列表获取")
    @GetMapping("/getList")
    public ResultResp getList(HttpServletRequest request){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            Map<String , Object> params = Util.request2Map(request);
            ResultResp = ResultResp.success(sysUserService.getList(user, params));
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
            ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }
}